<template>
	<view>
		<!-- 添加收货地址区域 -->
		<view class="address-btn-box" v-if="JSON.stringify(address) === '{}'">
			<button @click="chooseAddress" size="mini" type="primary" class="address-btn">请选择收货地址+</button>
		</view>
		
		<!-- 收货地址信息区域 -->
		<view class="address-ingo-box" v-else @click="chooseAddress">
			<view class="row1">
				<view class="row1-left">
					<view class="username">收货人：{{address.userName}}</view>
				</view>
				<view class="row1-right">
					<view class="phone">电话: {{address.telNumber}}</view>
					<uni-icons type="arrowright" size="16"></uni-icons >
				</view>
			</view>
			<view class="row2">
				<view class="row2-left">收货地址：</view>
				<view class="row2-right">{{addr}}</view>
			</view>
		</view>
		
		<!-- 底部边框线 -->
		<image src="/static/cart_border@2x.png" class="address-border"></image>
	</view>
</template>

<script>
	import { mapState,mapMutations,mapGetters } from 'vuex'
	export default {
		name:"my-address",
		computed: {
			...mapState('m_user',['address']),
			// 拼接地址信息
			...mapGetters('m_user',['addr'])
			
		},
		data() {
			return {
				// address:{}
			};
		},
		methods:{
			// 点击触发选择地址组件
			async chooseAddress(){
				// 返回值是数组 第一项为错误对象 没有错误则为null
				// 第二项为 success对象
				// 使用数组结构赋值
				const [err , suc] = await uni.chooseAddress().catch(err => err)
				if (err === null && suc.errMsg === 'chooseAddress:ok'){
					console.log(suc);
					// 用啥自己组件内数据 托管出去给store才是正途
					// this.address = suc
					// 通过vuex更新用户地址
					this.updateAddress(suc)
				}
				
				// 判断用户有无授权 (p.s 现在是默认直接授权了 无需判断了)
				// chooseAddress:fail authorize no response 为ios上的判断依据
				// chooseAddress:fail auth deny 为安卓和模拟器的判断依据
				if (err && (err.errMsg === 'chooseAddress:fail auth deny' || err.errMsg === 'chooseAddress:fail authorize no response')){
					// 通过调用这个方法 让用户重新授权
					// 否则 会出现用户没有授权 后续无法调用uni.chooseAddress
					this.reAuth()
				}
			},
			// 重新对uni.chooseAddress授权
			async reAuth(){
				const [err2,confirmResult] = await uni.showModal({
					content:'检测到您没有打开地址权限，是否去设置打开？',
					confirmText:'确认',
					cancelText:'取消'
				})
				
				if(err2) return
				
				if(confirmResult.cancel) return uni.$showMsg('您取消了地址授权')
				if(confirmResult.confirm) return uni.openSetting({
					success: (settingResult) => {
						if (!settingResult.authSetting['scope.address']) return uni.$showMsg('您取消了授权')
						if (settingResult.authSetting['scope.address']) return uni.$showMsg('授权成功！请选择收货地址')
					}
				})
			},
			...mapMutations('m_user',['updateAddress'])
			
		}
	}
</script>

<style lang="scss">
	.address-ingo-box{
		display: flex;
		flex-direction: column;
		justify-content: center;
		padding-left: 5px;
		margin-top: 10px;
		.row1{
			display: flex;
			justify-content: space-between;
			.row1-left{
			}
			.row1-right{
				display: flex;
			}
		}
		.row2{
			display: flex;
			margin-top: 10px;
			.row2-left{
				display: flex;
				align-items: center;
				white-space: nowrap;
			}
		}
	}
	
	.address-btn-box{
		display: flex;
		justify-content: center;
		align-items: center;
		height: 90px;
		.address-btn{
			
		}
	}
	.address-border{
		width: 100%;
		height: 5px;
	}
</style>
